<?php
$this->LoadHelper('TaskHelper');
$TaskHelper = new TaskHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('AttachHelper');
$AttachHelper = new AttachHelper($this->UserConfig, 'task');

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BranchHelper');
$BranchHelper = new BranchHelper();

$this->LoadResurces('oldapi/class.carloan');
$carloan = new Carloan();

$where = array(
    '`task_type` = ?' => 'ordersync',
    '`task_status` = ?' => 'no'
);

$task_data = $TaskHelper->taskAll($where);

//测试开关
$o_number = $this->Param['n'];
if($o_number) {
    $task_data = $OrderHelper->virtualTask($o_number, 'ordersync');
}

$mode = Config::GetCommon();
//REDIS 加锁 暂留
if(!empty($task_data)) foreach ($task_data as $k => $v){
    $order_sync = $OrderHelper->orderInfo($v['task_releid']);
    $attach_arr = $AttachHelper->imageList($v['task_releid']);
    $branch_row = $BranchHelper->branchRow($order_sync['base']['o_branch_id']);
    $order_sync['base']['o_branch_code'] = $branch_row['br_code'];
    $order_sync['base']['o_branch_payee'] = $branch_row['br_payee'];
    $order_sync['base']['o_branch_acc_bank'] = $branch_row['br_acc_bank'];
    $order_sync['base']['o_branch_acc_no'] = $branch_row['br_acc_no'];
    
    if(!empty($order_sync['attachment'])){
        foreach($order_sync['attachment'] as $key => $val){
            if(!isset($mode['attach_key'][$key]))                    continue;
            if(!empty($val)) foreach($val as $sk => $sv){
                if($key == 'oa_video'){
                    $attachment_row = $AttachHelper->fileRow(array('`af_path` = ?' => $sv));
                    $member_info = $MemberHelper->getMemberRow(array('`u_id` = ?' => $attachment_row['af_uid']));
                    $at_name = $attachment_row['af_title'];
                    $at_type = end(explode('.', $sv));
                    $at_size = $attachment_row['af_size'];
                    $at_time = $attachment_row['af_dateline'];
                    $at_user = $member_info['u_realname'];
                }else{
                    $attachment_row = $AttachHelper->imageRow(array('`ai_path` = ?' => $sv));
                    if(!empty($attachment_row)){
                        $member_info = $MemberHelper->getMemberRow(array('`u_id` = ?' => $attachment_row['ai_member']));
                        $at_name = $attachment_row['ai_title'];
                        $at_type = end(explode('.', $sv));
                        $at_size = $attachment_row['ai_size'];
                        $at_time = $attachment_row['ai_dateline'];
                        $at_user = $member_info['u_realname'];
                    }else{
                        $attachment = $AttachHelper->imageRow2($attach_arr,$sv);
                        $at_name = $attachment['image_name'];
                        $at_type = end(explode('.', $sv));
                        $at_size = 0;
                        $at_time = $attachment['created_at'];
                        $at_user = $attachment['user_name'];
                    }
                }
                $order_sync['attachment'][$key][$sk] = str_replace(BASE_ATTACH, 'Uploads/AppFtp', $sv);
                $order_sync['attachment_info'][$key][$sk] = array(
                    'at_name' => $at_name,
                    'at_type' => $at_type,
                    'at_size' => $at_size,
                    'at_time' => $at_time,
                    'at_user' => $at_user
                );                
            }
        }
    }
    $order_sync['base']['o_org_id'] = 1;
    $order_sync['base']['o_status'] = $order_sync['base']['o_status'] == '审核中' ? '初审提交' : $order_sync['base']['o_status'];
    $sync_result = $carloan->syncOrder($order_sync);
    $sync_result = json_decode($sync_result, true);
    if($o_number){
        echo print_r(array('r'=>$sync_result, 'd'=>$order_sync));
        exit;
    }
    else {
        if ($sync_result['success']) {
            $task_up = $TaskHelper->taskSave(array('task_status' => 'ok', 'task_dotime' => NOW_TIME, 'task_type' => $v['task_type']), $v['task_id']);
        }
        $TaskHelper->logSave(array(
            'tl_tid' => $v['task_id'],
            'tl_log' => serialize($sync_result),
            'tl_dateline' => NOW_TIME
        ));
    }
}
echo '执行完毕'.date('Y-m-d H:i:s');